//获取应用实例
var app = getApp()
const _request = require("../../utils/request.js"); // 引入require;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopimgUrl: app.globalData.shopimgUrl, 
    id: "",
    isOpen: false,
    starttime: "",
    endtime: "",
    workday: "",
    workdays: [{
        id: 1,
        val: '周一',
        checked: false
      },
      {
        id: 2,
        val: '周二',
        checked: false
      },
      {
        id: 3,
        val: '周三',
        checked: false
      },
      {
        id: 4,
        val: '周四',
        checked: false
      },
      {
        id: 5,
        val: '周五',
        checked: false
      },
      {
        id: 6,
        val: '周六',
        checked: false
      },
      {
        id: 7,
        val: '周日',
        checked: false
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getStoreSettingByStoreId();
    wx.enableAlertBeforeUnload({
      message: '内容未保存, 点击返回将导致未保存内容清空, 请确认是否返回?'
    })
  },
  // 获取店铺时间信息
  getStoreSettingByStoreId: function () {
    let storeId = wx.getStorageSync('storeId');
    _request.get('/storeSetting/getStoreSettingByStoreId', {
      storeId: storeId,
    }).then((res) => {
      this.setData({
        isOpen: res.data.data.open == 0 ? false : true,
        starttime: res.data.data.starttime.substring(0, 5),
        endtime: res.data.data.endtime.substring(0, 5),
        workday: res.data.data.workday,
        id: res.data.data.id,
      });
      let workdayArr = this.data.workday.split(",");
      let workdays = this.data.workdays;
      for (let i = 0; i < workdayArr.length; i++) {
        for (let j = 0; j < workdays.length; j++) {
          if (workdayArr[i] == workdays[j]['id']) {
            workdays[j]['checked'] = true;
          }
        }
      }
      this.setData({
        workdays: workdays
      })

    }).catch((error) => {
      console.log(error, 'error');
    })
  },
  bindStartTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      starttime: e.detail.value
    })
  },
  bindEndTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endtime: e.detail.value
    })
  },
  switch1Change: function (e) {
    let _this = this
    if (this.data.isOpen) {
      wx.showModal({
        title: '提示',
        content: '关闭营业开关后，门店将不再接受新的订单，有未完成核销的订单的话请及时处理！',
        success(res) {
          if (res.confirm) {
            _this.setData({
              isOpen: false
            })
          } else if (res.cancel) {
            _this.setData({
              isOpen: true
            })
          }
        }
      })
    } else {
      _this.setData({
        isOpen: e.detail.value
      })
    }
  },
  onWorkTap: function (e) {
    let id = e.currentTarget.dataset.item;
    let workdays = this.data.workdays;
    for (let j = 0; j < workdays.length; j++) {
      if (id == workdays[j]['id']) {
        workdays[j]['checked'] = workdays[j]['checked'] ? false : true;
      }
    }
    this.setData({
      workdays: workdays
    })
  },
  onAdd: function (e) {
    let storeId = wx.getStorageSync('storeId');
    let workday = "";
    let workdays = this.data.workdays;
    for (let j = 0; j < workdays.length; j++) {
      if (workdays[j]['checked']) {
        workday = workday + workdays[j]['id'] + ",";
      }
    }
    
    if (this.data.starttime >= this.data.endtime) {
      console.log(11111111111111)
      wx.showToast({
        title: '营业时间有误',
        icon: 'error',
        duration: 2000
      })
      return
    }
    workday = workday.substring(0, workday.length - 1);
    let open = this.data.isOpen ? 1 : 0;
    _request.postObj('/storeSetting/updateOrSave', {
      workday: workday,
      endtime: this.data.endtime,
      id: this.data.id,
      starttime: this.data.starttime,
      storeId: storeId,
      open: open,
    }).then((res) => {
      if(res.data.code == 200){
        wx.disableAlertBeforeUnload();
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        })
        wx.switchTab({
          url: '/firstpages/home/index',
        })
      } else{
        wx.showToast({
          title: res.data.message,
          icon: 'error',
          duration: 2000
        })
      }

    }).catch((error) => {
      console.log(error, 'error');
    })
  }
})